<?php 
class ControllerMyplanLogin extends Controller{
	public function index(){
		if($this->user->isLogged()){
			$this->redirect($this->url->link("myplan/myplanhome"));
		}
	
		$this->data["register"]=$this->url->link("myplan/register");
		$this->data["action"]=$this->url->link("myplan/login/login");
	
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/myplan/login.tpl')) 		            {
				$this->template = $this->config->get('config_template') . '/template/myplan/login.tpl';
			} else {
				$this->template = 'default/template/myplan/login.tpl';
			}
											
			$this->response->setOutput($this->render());
	}
	
	public function login(){
		
		if($this->request->server["REQUEST_METHOD"]=="POST"){
		
			$this->load->model("myplan/common");
		
				$validate=$this->validate();
				if($validate){
					$this->session->data["user_id"]=$validate["user_id"];
					$this->session->data["account"]=$validate["account"];
					$this->session->data["nickname"]=$validate["nickname"];
					$this->redirect($this->url->link("myplan/myplanhome"));
				}
				
				$this->index();
			
		}
		
	}
	
	public function validate(){
		if($this->request->post["account"]==""||$this->request->post["password"]==""){
			$this->data["warning"]="不能为空";
			return false;
		}
		
		$result=$this->model_myplan_common->login($this->request->post);
		
		if($result->num_rows==0){
			$this->data["warning"]="账号不存在";
			return false;
		}
		
		if($this->request->post["password"]!=$result->row["password"]){
			$this->data["warning"]="密码错误";
			return false;
		}
		
		return $result->row;
	}
}
?>